/**
 * Created by zhuhuijun on 2017-7-26 18:18:30
 */
app.controller('TemplateListCtrl', TemplateListCtrl);
TemplateListCtrl.$inject = ['dbprofileService', 'ngTableService', 'toaster'];
function TemplateListCtrl(dbprofileService, ngTableService, toaster) {
    var vm = this;
    //all function here
    //-----------------
    vm.init = init;
    vm.add = add;
    vm.submit = submit;
    vm.update = update;
    vm.choice = choice;
    vm.connectTest = connectTest;
    vm.remove = remove;
    //all view object here
    //------------------
    vm.action = null;
    vm.dbprofile = {};
    //all function call here
    //-----------------------
    function init() {
        dbprofileService.findAll().then(function (response) {
            vm.tableParam = ngTableService.init(response.data)
        })
    }

    function submit() {
        var param = {dbprofile: vm.dbprofile};
        if (vm.action == "add") {
            dbprofileService.save(param).then(function (response) {
                layer.closeAll('page');
                init();
            });
        } else if (vm.action == "update") {
            dbprofileService.update(param).then(function (response) {
                layer.closeAll('page');
                init();
            });
        }
        if (vm.row) vm.row.checked = false;
        vm.dbprofile = {};
    }

    function add() {
        vm.dbprofile = {};
        vm.action = "add";
        if (vm.row) vm.row.checked = false;
        layerOpen("新增配置信息", "addInfo");
    }

    function remove(id) {
        dbprofileService.remove(id).then(function (response) {
            init();
        })
    }

    function update() {
        vm.dbprofile = vm.row;
        vm.action = "update";
        layerOpen("更新配置信息", "addInfo");
    }

    function choice(row) {
        vm.row = row;
        row.checked = true;
    }

    function connectTest(formObj) {
        var param = {dbprofile: formObj};
        dbprofileService.connectTest(param).then(function (response) {
            toaster.pop("success","连接测试","连接成功!");
        });
    }

    function layerOpen(title, contentId) {
        layer.open({
            type: 1
            , area: ['750px', '500px']
            , title: title
            , shade: 0.6
            , maxmin: true
            , anim: 1
            , content: $("#" + contentId)
        });
    }
}
